<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tests extends Backend_Controller {

	protected $_title = 'Ujian / Ulangan';
	
	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="teacher")
			redirect('login/index/1');
		$this->load->model('school_years_model', 'school_years', true);
		$this->load->model('teachings_model', 'teachings', true);
		$this->load->model('classes_model', 'classes', true);
		$this->load->model('test_question_model', 'test_question', true);
	}
		
	public function index($offset=0, $sort_by='name') {
		
		$data = array(
			'mainview' => 'index',
		) + $this->_get_header_data();
		
		$this->load->view('teacher/template',$data);
		
	}
	
	function _crud_output($output = null) {
        $this->load->view('teacher/template_crud.php',$output);    
    }

	function tests_display(){	
		$crud = new grocery_CRUD();
        $crud->set_model('tests_crudmodel');
		$crud->set_theme('flexigrid');
        $crud->set_table('tests');
        $crud->set_subject('Soal Ujian');
		
		$crud->columns('date','information','class_id','question_id','percentage','total_qty','duration','maximum_value','maximum_skip','start');
		$crud->display_as('date','Tanggal Pembuatan');
		$crud->display_as('information','Keterangan');
		$crud->display_as('class_id','Untuk Kelas');
		$crud->display_as('question_id','Sumber Soal');
		$crud->display_as('percentage','Komposisi Soal');
		$crud->display_as('total_qty','Total Soal');
		$crud->display_as('duration','Durasi Soal (menit)');
		$crud->display_as('maximum_value','Nilai Maksimum');
		$crud->display_as('maximum_skip','Total Skip');
		$crud->display_as('start','Publish');
		
		$crud->fields('information','class_id','question_id','percentage','total_qty','duration','maximum_value','maximum_skip','start');
		$crud->required_fields('information','class_id','question_id','percentage','total_qty','duration','maximum_value');
		
		$rs = $this->school_years->get_active_school_year();
		
		$dataclass = $this->teachings->lists_class_array($rs[0]['id'], $_SESSION['person_id'], $_SESSION['subject_id']);
		$dataquestions = $this->classes->questions_lists_array($rs[0]['id'], $_SESSION['person_id'], $_SESSION['subject_id']);
		$datapercentage = array(100=>'100%',90=>'90%',80=>'80%',70=>'70%',60=>'60%',51=>'50%',50=>'50%',40=>'40%',30=>'30%',20=>'20%',10=>'10%',0=>'0%');
		
		$crud->change_field_type('class_id','multiselect',$dataclass);
		$crud->change_field_type('question_id','multiselect',$dataquestions);
		$crud->change_field_type('percentage','set',$datapercentage);
		$crud->change_field_type('total_qty','integer');
		$crud->change_field_type('duration','integer');
		$crud->change_field_type('maximum_value','integer');
		$crud->change_field_type('maximum_skip','integer');
		$crud->change_field_type('start','true_false');
		$crud->callback_before_insert(array($this, 'insert_data'));
		
		$crud->callback_add_field('maximum_skip',array($this,'callback_edit_maximum_skip'));
		$crud->callback_add_field('start',array($this,'callback_edit_start'));
		$crud->callback_edit_field('start',array($this,'callback_edit_start'));

		$crud->set_rules('question_id', 'Sumber Soal','callback_simpan_sumber_soal');
		$crud->set_rules('percentage', 'Prosentase','callback_percentage_check');
		$crud->set_rules('total_qty', 'Jumlah Soal','callback_total_qty_check');
        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	function insert_data($post_array)
	{
		$rs = $this->school_years->get_active_school_year();
		$post_array['school_year_id'] = $rs[0]['id'];
		$post_array['date'] = date('Y-m-d');
		$post_array['time'] = date('H:i:s');
		$post_array['ip'] = $_SERVER['REMOTE_ADDR'];
		//echo var_dump($post_array);
		return $post_array;
	}
	
	function callback_edit_maximum_skip($value){
		return	'<input type="text" value=0 name="maximum_skip">';
	}
	
	function callback_edit_start($value){
		$retval = '';
		if($value==0)
			$retval = '<input type="radio" value=0 name="start" checked> Tidak <input type="radio" value=1 name="start"> Ya';
		else
			$retval = '<input type="radio" value=0 name="start"> Tidak <input type="radio" value=1 name="start" checked> Ya';
		return $retval;
	}
	
	public function percentage_check($str){
		$retval=FALSE;
		$retval2=FALSE;
		if($_SESSION[chapter_total] == count($str)) {
			$retval = TRUE;
		}
		else{
			$retval = FALSE;
		}
		
		$total = 0;
		foreach($str as $value)
			$total = $total + ((int)$value);
		
		if($total==100){
			$retval2 = TRUE;
		}
		else{
			$retval2 = FALSE;
		}
		
		if($retval==TRUE && $retval2==TRUE){
			$_SESSION['percentage'] = '';
			foreach($str as $value)
				$_SESSION['percentage'] = $_SESSION['percentage'] . $value . ",";
			return TRUE;
		}
		else{
			$this->form_validation->set_message('percentage_check', 'Jumlah <b>komposisi soal</b> harus = jumlah <b>sumber soal</b> dan harus = 100%.');
			return FALSE;
		}
	}
	
	public function total_qty_check($str){	
	
		$retval = true;
		$question_id = explode(",",$_SESSION[question_id]);
		$percentage = explode(",", $_SESSION[percentage]);
		for($a=0; $a< count($question_id)-1; $a++)
		{
			$total = $this->test_question->get_total_qty($question_id[$a], $percentage[$a], $str);
			if($total < 0)
			{
				$retval = false;
				break;
			}
			
		}
		
		if($retval == false){
			$this->form_validation->set_message('total_qty_check', 'Jumlah soal dalam bank soal (untuk masing-masing komposisi soal) <b>tidak boleh kurang dari</b> (komposisi soal x total soal ujian).');
			return false;
		}
		else
			return true;
	}
	
	public function simpan_sumber_soal($str)
	{
		$_SESSION['chapter_total'] = count($str);
		$_SESSION['question_id'] = '';
		foreach($str as $value)
			$_SESSION['question_id'] = $_SESSION['question_id'] . $value . ",";
		return true;
	}
}
